<?php
/**
 * @title 2258. 逃离火灾
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution
{

    /**
     * @param Integer[][] $grid
     * @return Integer
     */
    function maximumMinutes($grid)
    {
        /**
         * @since 2023-11-09 格子范围
         */
        $m = count($grid);
        $n = count($grid[0]);

        /**
         * @since 2023-11-09 目前火点
         */
        $firePointArray = [];
        for($x=0; $x<$m; $x++){
            for($y=0; $y<$n; $y++){
                if($grid[$x][$y] == 1){
                    $firePointArray[] = [$x, $y];
                } else {}
            }
        }

        /**
         * @since 2023-11-09 记录每分钟-火点分布情况
         */
        $fireGridArray = [$grid];
        $firePointCnt = count($firePointArray);
        while(true){
            /**
             * @since 2023-11-09 新的火点
             */
            $nextFirePointArray = [];

            /**
             * @since 2023-11-09 迭代目前火点
             */
            foreach ($firePointArray as list($x, $y)){
                /**
                 * @since 2023-11-09 向上
                 */
                if($x-1>=0){
                    if($grid[$x-1][$y] == 0){
                        $grid[$x-1][$y] = 1;
                        $nextFirePointArray[] = [$x-1, $y];
                    } else {}
                } else {}

                /**
                 * @since 2023-11-09 向下
                 */
                if($x+1<$m){
                    if($grid[$x+1][$y] == 0){
                        $grid[$x+1][$y] = 1;
                        $nextFirePointArray[] = [$x+1, $y];
                    } else {}
                }

                /**
                 * @since 2023-11-09 向左
                 */
                if($y-1>=0){
                    if($grid[$x][$y-1] == 0){
                        $grid[$x][$y-1] = 1;
                        $nextFirePointArray[] = [$x, $y-1];
                    } else {}
                }

                /**
                 * @since 2023-11-09 向右
                 */
                if($y+1<$n){
                    if($grid[$x][$y+1] == 0){
                        $grid[$x][$y+1] = 1;
                        $nextFirePointArray[] = [$x, $y+1];
                    } else {}
                }
            }

            /**
             * @since 2023-11-09 是否存在新的火点
             */
            if($nextFirePointArray){
                $fireGridArray[] = $grid;
                $firePointArray = $nextFirePointArray;

                /**
                 * @since 2023-11-09 终点起火
                 */
                if($grid[$m-1][$n-1] == 1){
                    break;
                } else {}
            } else {
                break;
            }
        }


    }
}

/**
 * @url http://127.0.0.1/leetcode/202311/2023.11.09.php
 */
$datas = [
    // [[[0,2,0,0,0,0,0],[0,0,0,2,2,1,0],[0,2,0,0,1,2,0],[0,0,2,2,2,0,2],[0,0,0,0,0,0,0]]], // 3
    [[[0,0,0,0],[0,1,2,0],[0,2,0,0]]], // -1
    // [[[0,0,0],[2,2,0],[1,2,0]]], // 10^9
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->maximumMinutes(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
